﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using OnlineStore.Dal;
using OnlineStore.Bll;

namespace OnlineStore.Gui
{
    public partial class Register : System.Web.UI.Page
    {
        string Pass,passAgain;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["user"] != null) Response.Redirect("/",false);
            if (IsPostBack)
            {
                Pass = tb_Pass.Text;
                passAgain = tb_PassAgain.Text;
                lb_warning_UserName.Visible = DalUser.isUser(tb_UserName.Text);

                if (passAgain != Pass || Pass == "")
                    lb_warning_pass.Visible = true;
                else
                    lb_warning_pass.Visible = false;
            }
            if (Session["user"] != null)
            {
                HL_tab_login.NavigateUrl = "/gui/Account.aspx";
                HL_tab_login.Text = ((tbCustomer)Session["user"]).Username;
                HL_tab_Logout.Visible = true;
            }
            /*Giỏ hàng bên phải*/
            ListCartRight.DataSource = (List<bizCart>)Session["cart"];
            ListCartRight.DataBind();
            lb_Sum.Text = String.Format("{0:0,0 VNĐ}", bizCart.SumAllList((List<bizCart>)Session["cart"]));
            lb_Sum_product.Text = String.Format("{0:0,0 VNĐ}", bizCart.PriceOrder(bizCart.SumAllList((List<bizCart>)Session["cart"])));
            /**/
        }

        protected void Onsubmit(object sender, EventArgs e)
        {
            if (tb_FullName.Text == "" || tb_UserName.Text == "" || passAgain != Pass || Pass == "" || tb_Tel.Text == "" || tb_Tel.Text ==""||tb_Address.Text == "")
            {
                lb_warning.Visible = true;
                return;
            }
            bizUser _bizUser = new bizUser();
            tbCustomer user = _bizUser.RegisterUser(tb_UserName.Text, Pass, tb_FullName.Text, tb_Tel.Text, tb_Address.Text);
            if (user != null) 
            {
                Session["user"] = user;
                Response.Redirect("/");
            }
        }
    }
}